package com.myapp.ruyixing.ui.shopmanager;

import static android.view.View.GONE;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

import com.myapp.ruyixing.R;
import com.myapp.ruyixing.http.RYHttpClient;
import com.myapp.ruyixing.http.RYHttpResponseHandler;
import com.myapp.ruyixing.model.ActivityOrderInfo;
import com.myapp.ruyixing.model.Model;
import com.myapp.ruyixing.ui.BaseActivity;
import com.myapp.ruyixing.ui.widget.TitleBar;
import com.myapp.ruyixing.utils.LogUtils;
import com.myapp.ruyixing.utils.LoginHelper;

public class ParticipantDetailActvity extends BaseActivity implements
		OnClickListener {

	private String mDataId;
	private String mAId;
	private ActivityOrderInfo mInfo;

	private TextView mNameView;
	private TextView mOrderStateView;
	private TextView mTimeView;
	private TextView mSexView;
	private TextView mPhoneView;
	private TextView mMessageView;
	private TextView mRemarkView;
	private TextView mPointView;
	private TitleBar mTitleBar;
	private View mLoadingView;
	private View mContentView;

	private RYHttpResponseHandler mHttpResponseHandler = new RYHttpResponseHandler() {

		@Override
		public void onSuccess(Object o) {
			try {
				ActivityOrderInfo info = (ActivityOrderInfo) o;
				onLoadingDataFinished(true, info);
			} catch (Exception e) {
				LogUtils.LOGW("ParticipantDetailActvity", e.getMessage());
				onLoadingDataFinished(false, null);
			}
		}

		@Override
		public void onFailure(Throwable throwable) {
			onLoadingDataFinished(false, null);
		}
	};

	private void onLoadingDataStarted(String shopbduss, String dataid) {

		showLoading();
		RYHttpClient.getActivityOrderInfo(this, mHttpResponseHandler, dataid,
				shopbduss);
	}

	private void onLoadingDataFinished(boolean succeed, ActivityOrderInfo info) {
		hideLoading();
		if (succeed && info != null) {
			mInfo = info;
			mContentView.setVisibility(View.VISIBLE);
			mMessageView.setText("留言：" + info.content);
			mNameView.setText("姓名：" + info.name);
			mOrderStateView.setText(info.status);
			mPhoneView.setText("电话：" + info.mobile);
			mPointView.setText("赠送积分：" + info.score);
			mRemarkView.setText("商家备注：" + info.remark);
			mSexView.setText("性别：" + info.sex);
			mTimeView.setText(info.time);
		} else {
			mContentView.setVisibility(GONE);
			Toast.makeText(this, "加载失败" + (info == null ? "" : info.msg),
					Toast.LENGTH_SHORT).show();
		}
	}

	private RYHttpResponseHandler mDeleteHttpResponseHandler = new RYHttpResponseHandler() {

		@Override
		public void onSuccess(Object o) {
			try {
				Model resul = (Model) o;
				onDelteFinished(true, resul);
			} catch (Exception e) {
				onDelteFinished(false, null);
			}
		}

		@Override
		public void onFailure(Throwable throwable) {
			onDelteFinished(false, null);
		}
	};

	private void onDeleteStarted(String shopbduss, String dataid) {
		showLoading();
		RYHttpClient.delActivityOrder(this, mDeleteHttpResponseHandler,
				shopbduss, dataid);
	}

	private void onDelteFinished(boolean succeed, Model result) {
		hideLoading();
		if (succeed && result != null && result.errno == 0) {
			Toast.makeText(this, result.msg, Toast.LENGTH_SHORT).show();
			finish();
		} else {
			Toast.makeText(this, "删除失败:" + (result == null ? "" : result.msg),
					Toast.LENGTH_SHORT).show();
		}
	}

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);

		setContentView(R.layout.activity_shop_manager_participant_detail);

		initView();

		mDataId = getIntent().getStringExtra("dataid");
		mAId = getIntent().getStringExtra("aid");
	}

	@Override
	protected void onResume() {
		onLoadingDataStarted(LoginHelper.getInstance().getShopBduss(), mDataId);
		super.onResume();
	}

	private void initView() {
		mContentView = findViewById(R.id.layout_content);

		findViewById(R.id.update_status).setOnClickListener(this);
		findViewById(R.id.notify_user).setOnClickListener(this);
		findViewById(R.id.delete).setOnClickListener(this);

		mNameView = (TextView) findViewById(R.id.name);
		mOrderStateView = (TextView) findViewById(R.id.order_state);
		mTimeView = (TextView) findViewById(R.id.time);
		mSexView = (TextView) findViewById(R.id.sex);
		mPhoneView = (TextView) findViewById(R.id.phone);
		mMessageView = (TextView) findViewById(R.id.message);
		mRemarkView = (TextView) findViewById(R.id.remark);
		mPointView = (TextView) findViewById(R.id.point);

		mTitleBar = (TitleBar) findViewById(R.id.title_bar);
		mLoadingView = findViewById(R.id.my_loading);
		mTitleBar.setOnIconClickedListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				onBackPressed();
			}
		});

	}

	private void showTitleBarLoading() {
		mTitleBar.showLoading(true);
	}

	private void hideTitleBarLoading() {
		mTitleBar.showLoading(false);
	}

	private void showLoading() {
		mLoadingView.setVisibility(View.VISIBLE);
	}

	private void hideLoading() {
		mLoadingView.setVisibility(View.GONE);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.update_status:
			startActivity(new Intent(this, ActivityUpdateStatusActivity.class)
					.putExtra("status", mInfo.status.trim())
					.putExtra("remark", mInfo.remark.trim())
					.putExtra("dataid", mDataId).putExtra("aid", mAId));
			break;
		case R.id.notify_user:
			startActivity(new Intent(this, ActivityNotifyUserActivity.class)
					.putExtra("name", mInfo.name.trim())
					.putExtra("mobile", mInfo.mobile.trim())
					.putExtra("dataid", mDataId).putExtra("aid", mAId));
			break;
		case R.id.delete:
			onDeleteStarted(LoginHelper.getInstance().getShopBduss(), mDataId);

			break;

		default:
			break;
		}
	}
}
